﻿using AutoMapper;
using MediatR;
using NPOI.XSSF.UserModel;
using SmartConference.Api.Applocations.IServices;
using SmartConference.Api.DTO;
using SmartConference.Api.Filter;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Interfaces;
using System.ComponentModel;

namespace SmartConference.Api.Applocations.Services
{
    public class UserServices : IUserServices
    {
        private readonly IUserRepository userRepository;
        private readonly IMapper mapper;
        private readonly JWTHelper jwtHelper;

        public UserServices(IUserRepository userRepository,IMapper mapper, JWTHelper jwtHelper)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
            this.jwtHelper = jwtHelper;
        }

        public async Task<ApiResult> DeleteUser(long IDNumber)
        {
            try
            {
                var list = await userRepository.GetAsync(x => x.IDNumber == IDNumber);
                userRepository.DeleteAsync(list);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<List<UserInfo>> ExportUser()
        {
            try
            {
                var list=userRepository.GetAll().ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult<UserLoginDTO>> Login([DefaultValue("admin")]string UserNumber, [DefaultValue("123456")] string UserPassword)
        {
            try
            {
                var userinfo = await userRepository.GetAsync(x => x.UserNumber == UserNumber);
                if (userinfo == null)
                {
                    return ApiResult<UserLoginDTO>.Error(ApiResultCode.Error, "用户不存在");
                }
                if (userinfo.UserPassword != UserPassword)
                {
                    return ApiResult<UserLoginDTO>.Error(ApiResultCode.Error, "密码错误");
                }
                var logininfo=mapper.Map<UserLoginDTO>(userinfo);
                logininfo.Jwt = jwtHelper.CreateToken(logininfo.IDNumber,logininfo.UserNumber);
                return ApiResult<UserLoginDTO>.Success(ApiResultCode.Success, logininfo);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult<ApiPaging<UserInfo>>> ShowUser(long? IDNumber, string? MemberLevel, DateTime? CreateTime, DateTime? EndTime, [DefaultValue(1)] int pageIndex, [DefaultValue(10)] int pageSize)
        {
            try
            {
                var userlist = userRepository.GetAll();
                if (IDNumber != null)
                {
                    userlist = userlist.Where(x => x.IDNumber == IDNumber);
                }
                if (MemberLevel != null)
                {
                    userlist = userlist.Where(x => x.MemberLevel.Contains(MemberLevel));
                }
                if(CreateTime != null&&EndTime != null)
                {
                    userlist = userlist.Where(x => x.CreateTime >= CreateTime&&x.CreateTime<=EndTime.Value.AddDays(1));
                }
                int totalcount = userlist.Count();
                int pagecount = (int)(Math.Ceiling(totalcount * 1.0 / pageSize));
                userlist = userlist.OrderBy(x => x.IDNumber).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                ApiPaging<UserInfo> paging = new ApiPaging<UserInfo>(totalcount, pagecount, userlist.ToList());
                return ApiResult<ApiPaging<UserInfo>>.Success(ApiResultCode.Success, paging);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult> UpdateUser(UserUpdateInfoDTO userUpdateInfoDTO)
        {
            try
            {
                var userinfo=await userRepository.GetAsync(x=>x.IDNumber==userUpdateInfoDTO.IDNumber);
                userinfo.PhoneNumber = userUpdateInfoDTO.PhoneNumber;
                userinfo.MemberLevel = userUpdateInfoDTO.MemberLevel;
                userinfo.Sex = userUpdateInfoDTO.Sex;
                userinfo.Birthday = userUpdateInfoDTO.Birthday;
                userinfo.LiveCity = userUpdateInfoDTO.LiveCity;
                userinfo.Occupation = userUpdateInfoDTO.Occupation;
                userinfo.PersonalizedDesc = userUpdateInfoDTO.PersonalizedDesc;
                await userRepository.UpdateAsync(userinfo);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
